package cn.tanfp.dynamicdatasource.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration("mapperConfig")
@MapperScan(basePackages ={"cn.tanfp.dynamicdatasource.mapper"},sqlSessionFactoryRef = "sqlSessionFactoryBean") //扫描的mapper
public class MapperConfig {

    @Bean
    public DruidDataSource masterDruidDataSource(){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3339/tfp");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    @Bean
    public DruidDataSource slave1DruidDataSource(){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3340/tfp");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    @Bean
    public DruidDataSource slave2DruidDataSource(){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3341/tfp");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    @Bean
    public DynamicDataSource dynamicDataSource(){
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 设置默认数据源
        dynamicDataSource.setDefaultTargetDataSource(masterDruidDataSource());

        // 设置全部的数据源
        Map map=new HashMap();
        map.put(DynamicDataSource.MASTER,masterDruidDataSource());
        map.put(DynamicDataSource.SLAVE1,slave1DruidDataSource());
        map.put(DynamicDataSource.SLAVE2,slave2DruidDataSource());
        dynamicDataSource.setTargetDataSources(map);
        return dynamicDataSource;
    }


    @Bean
    public SqlSessionTemplate sqlSessionTemplate(){
        SqlSessionTemplate sqlSessionTemplate=null;
        try {
            sqlSessionTemplate=new SqlSessionTemplate(sqlSessionFactoryBean().getObject());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  sqlSessionTemplate;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dynamicDataSource());
        // 设置mapper.xml扫描
        // (一定不能在application.yml中配置，因为我们现在是用的自己的mybatis配置，application.yml中的配置是失效状态，要配置属性，只能在这个配置类中配置)
        sqlSessionFactoryBean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mappers/*.xml"));
        // 设置返回值pojo的别名包
        sqlSessionFactoryBean.setTypeAliasesPackage("cn.tanfp.dynamicdatasource.pojo");
        return sqlSessionFactoryBean;
    }
}
